//
//  LocAnnotationView.m
//  carcareIOS
//
//  Created by ileo on 15/7/17.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "LocAnnotationView.h"

#define kArrorHeight 10

#define LEFT 170
#define RIGHT 50

@interface LocRightClickCalloutView()

@property (nonatomic, strong) UILabel *titleL;
@property (nonatomic, strong) UILabel *subTitleL;

@end

@implementation LocRightClickCalloutView

-(instancetype)initWithFixedSize{
    self = [super initWithSize:CGSizeMake(220, 65)];
    if (self) {
        CGFloat height = self.height - kArrorHeight;
        self.backgroundColor = [UIColor clearColor];
        self.titleL = [Factory labelWithFrame:CGRectMake(10, 10, LEFT-20, height/3) font:FONT_DEFAULT_Light(14) text:nil textColor:COLOR_HEXSTRING(@"#181818") onView:self textAlignment:NSTextAlignmentLeft];
        self.subTitleL = [Factory labelWithFrame:CGRectMake(10, self.titleL.bottom, LEFT-20, height/3) font:FONT_DEFAULT_Light(12) text:nil textColor:COLOR_HEXSTRING(@"#969493") onView:self textAlignment:NSTextAlignmentLeft];
        __weak __typeof(self) wself = self;
        [Factory imageViewWithCenter:CGPointMake(LEFT + RIGHT/2, height/3) image:ImageNamed(@"loc_wayto") onView:self];
        [Factory labelWithFrame:CGRectMake(LEFT, height/3, RIGHT, height/3*2) font:FONT_DEFAULT_Light(11) text:@"导航" textColor:[UIColor whiteColor] onView:self textAlignment:NSTextAlignmentCenter];
        [[UIButton buttonEmptyWithFrame:CGRectMake(LEFT, 0, RIGHT, height) click:^{
            if (wself.RightClick) {
                wself.RightClick();
            }
        }] setupOnView:self];
    }
    return self;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleL.text = title;
}

-(void)setSubTitle:(NSString *)subTitle{
    _subTitle = subTitle;
    self.subTitleL.text = subTitle;
}

- (void)drawRect:(CGRect)rect{
    [self drawInContext:UIGraphicsGetCurrentContext()];
    self.layer.shadowRadius = 0.5;
    self.layer.shadowOpacity = 1;
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}
- (void)drawInContext: (CGContextRef)context{
    CGContextSetLineWidth(context, 2.0);
    [self drawLeftInContext:context];
    [self drawRightInContext:context];
}
- (void)drawLeftInContext:(CGContextRef)context{
    CGRect rect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rect),
            midx = CGRectGetMidX(rect),
            maxx = minx + LEFT,
            miny = CGRectGetMinY(rect),
            maxy = CGRectGetMaxY(rect)-kArrorHeight;
    
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddLineToPoint(context, maxx, miny);
    CGContextAddLineToPoint(context, maxx, maxy);

    CGContextClosePath(context);
    CGContextFillPath(context);

}
- (void)drawRightInContext:(CGContextRef)context{
    CGRect rect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rect) + LEFT,
    maxx = CGRectGetMaxX(rect),
    miny = CGRectGetMinY(rect),
    maxy = CGRectGetMaxY(rect)-kArrorHeight;
    
    CGContextSetFillColorWithColor(context, [Theme mainColor].CGColor);
    
    CGContextMoveToPoint(context, minx, miny);
    
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, minx, maxy, radius);
    CGContextAddLineToPoint(context, minx, maxy);
    
    CGContextClosePath(context);
    CGContextFillPath(context);
}

@end

@interface LocSimpleCalloutView()

@property (nonatomic, strong) UILabel *titleL;

@property (nonatomic , strong) UILabel * subTitleL;

@end

@implementation LocSimpleCalloutView

+(CGSize)size{
    return CGSizeMake(140, 54);
}

-(instancetype)initWithFixedSize{
    self = [super initWithSize:CGSizeMake(140, 54)];
    if (self) {
        CGFloat height = self.height - kArrorHeight;
        self.backgroundColor = [UIColor clearColor];
        self.titleL = [Factory labelWithFrame:CGRectMake(10, 5, self.width-20, height) font:FONT_DEFAULT_Light(12) text:nil textColor:[Theme wordGrayColor] onView:self textAlignment:NSTextAlignmentLeft];
        self.subTitleL = [Factory labelWithFrame:CGRectMake(self.subTitleL.left, self.titleL.bottom, self.titleL.width, self.titleL.height) font:FONT_DEFAULT_Light(14) text:nil textColor:[Theme wordColor] onView:self textAlignment:NSTextAlignmentLeft];
    }
    return self;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleL.text = title;
    UIFont * titleFont = FONT_DEFAULT_Light(12);
    NSDictionary *attributes = @{NSFontAttributeName:titleFont};
    CGSize labelSize = [title boundingRectWithSize:CGSizeMake(self.titleL.frame.size.width , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        [self.titleL setSize:CGSizeMake(self.subTitleL.frame.size.width, labelSize.height)];
    [self.subTitleL setTop:self.titleL.bottom];
    [self.subTitleL setLeft:self.titleL.left];
}
- (void)setSubTitle:(NSString *)subTitle
{
    _subTitle = subTitle;
    self.subTitleL.text = subTitle;
    UIFont * titleFont = FONT_DEFAULT_Light(14);
    NSDictionary *attributes = @{NSFontAttributeName:titleFont};
    CGSize labelSize = [subTitle boundingRectWithSize:CGSizeMake(self.subTitleL.frame.size.width , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    [self.subTitleL setSize:CGSizeMake(self.subTitleL.frame.size.width, labelSize.height)];
    CGFloat viewHeight = labelSize.height + self.titleL.bottom + 10 + 5;
    [self setFrame:CGRectMake(self.frame.origin.x, (viewHeight > 54 ? self.frame.origin.y - (viewHeight - 54) : self.frame.origin.y), self.width, (viewHeight > 54 ? viewHeight : self.frame.size.height))];
    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect{
    [self drawInContext:UIGraphicsGetCurrentContext()];
    self.layer.shadowRadius = 0.5;
    self.layer.shadowOpacity = 1;
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}

- (void)drawInContext: (CGContextRef)context{
    CGContextSetLineWidth(context, 2.0);
    
    CGRect rect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rect),
            midx = CGRectGetMidX(rect),
            maxx = CGRectGetMaxX(rect),
            miny = CGRectGetMinY(rect),
            maxy = CGRectGetMaxY(rect)-kArrorHeight;
    
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);

    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, minx, maxy, radius);
    
    CGContextDrawPath(context, kCGPathFill);
    
    CGContextClosePath(context);
    CGContextFillPath(context);
}

@end

@interface LocAnnotationView()

@end

@implementation LocAnnotationView

+(UIView *)simpleCalloutView{
    return [[LocSimpleCalloutView alloc] initWithFixedSize];
}

+(UIView *)rightClickCalloutViewWithRightClick:(void (^)())click{
    LocRightClickCalloutView *calloutView = [[LocRightClickCalloutView alloc] initWithFixedSize];
    calloutView.RightClick = click;
    return calloutView;
}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
    if (self.selected == selected) {
        return;
    }
    if (self.Select && selected) {
        self.Select();
    }
    if (self.calloutView) {
        if (selected) {
            self.calloutView.center = CGPointMake(self.width/2 + self.calloutOffset.x, -self.calloutView.height/2 + self.calloutOffset.y);
            [self addSubview:self.calloutView];
            if (self.CalloutViewUpdate) {
                self.CalloutViewUpdate(self.calloutView,self.annotation);
            }
        }else{
            [self.calloutView removeFromSuperview];
        }
    }
    [super setSelected:selected animated:animated];
}

@end
